package my.ts.code;

import my.ts.code.listener.ConfirmListener;
import my.ts.code.net.AppContext;
import my.ts.code.objects.Root;
import my.ts.code.util.Util;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DescriptionDetailScreen extends BaseActivity implements
		OnClickListener, ConfirmListener {

	TextView title, description;
	ImageView image;
	Button download, gift;
	Root root;
	String sid = "", strTitle;
	int id;
	Drawable d = null;
	Context myContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.description_screen);

		setCListener(this);
		myContext = this;

		init();

		showDialogLoading(this, "Loading Data");

		getData(id, sid);
	}

	@Override
	public void onResume() {

		super.onResume();
		AppContext.instance.session.setListener(this);

	}

	public void getData(int id, String sid) {
		switch (id) {
		case Config.GAME:
			AppContext.instance.session.getGameDescription(sid);
			break;
		case Config.POSTCARD:
			AppContext.instance.session.getPostCardDetail(sid);
			break;

		default:
			break;
		}
	}

	public void init() {
		Bundle b = getIntent().getExtras();
		sid = b.getString("sid");
		id = b.getInt("id");
		strTitle = b.getString("title");

		title = (TextView) findViewById(R.id.title);
		title.setText(strTitle);

		description = (TextView) findViewById(R.id.description);

		image = (ImageView) findViewById(R.id.image);

		download = (Button) findViewById(R.id.download);
		download.setOnClickListener(this);

		gift = (Button) findViewById(R.id.gift);
		gift.setOnClickListener(this);

	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				description.setText(root.getItems().get(0).getDescription());
			} else if (msg.what == 2) {
				image.setImageDrawable(d);
			}
		}
	};

	@Override
	public void dispatch(String data) {
		// TODO Auto-generated method stub
		// System.out.println("ON DISPATCH");

		dialog.dismiss();

		root = parse(data);

		if (root != null) {
			Message msg = handler.obtainMessage(1);
			handler.sendMessage(msg);
			getImage(root.getItems().get(0).getImageURL());
		}
	}

	private void getImage(final String link) {
		Thread t = new Thread() {
			public void run() {
				d = Util.getImage(link);
				if (d != null) {
					Message msg = handler.obtainMessage(2);
					handler.sendMessage(msg);
				}
			}
		};
		t.start();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.download:
			downdoand(id);
			break;

		case R.id.gift:
			donate(id);
			break;

		default:
			break;
		}
	}

	private void downdoand(int id) {
		switch (id) {
		case Config.GAME:
			showDialogConfirmSMS(myContext, DescriptionDetailScreen.this,
					"Bạn có đồng ý gửi tin nhắn để tải game?", "Tải game",
					Config.GAME_SYNTAX.getSyntax()
							+ root.getItems().get(0).getSMSDownloadCode(),
					Config.GAME_SYNTAX.getNumber(), false, false);
			break;

		case Config.POSTCARD:
			showDialogConfirmSMS(myContext, DescriptionDetailScreen.this,
					"Bạn có đồng ý gửi tin nhắn để tải thiệp nhạc?", "Tải thiệp nhạc",
					Config.POSTCARD_SYNTAX.getSyntax()
							+ root.getItems().get(0).getSMSDownloadCode(),
					Config.POSTCARD_SYNTAX.getNumber(), false, false);
			break;

		default:
			break;
		}
	}

	private void donate(int id){
		switch (id) {
		case Config.GAME:
			showDialogConfirmSMS(myContext, DescriptionDetailScreen.this,
					"Bạn có đồng ý gửi tin nhắn để tặng game?", "Tặng game",
					Config.GAME_SYNTAX.getSyntax()
							+ root.getItems().get(0).getSMSDownloadCode(),
					Config.GAME_SYNTAX.getNumber(), true, false);
			break;

		case Config.POSTCARD:
			showDialogConfirmSMS(myContext, DescriptionDetailScreen.this,
					"Bạn có đồng ý gửi tin nhắn để tặng thiệp nhạc?", "Tặng thiệp nhạc",
					Config.POSTCARD_SYNTAX.getSyntax()
							+ root.getItems().get(0).getSMSDownloadCode(),
					Config.POSTCARD_SYNTAX.getNumber(), true, false);
			break;

		default:
			break;
		}
	}
	
	@Override
	public void confirmListener(boolean done) {
		// TODO Auto-generated method stub
		
	}
}
